package senzuri.acceleraudio.ui;

import java.util.Arrays;

import senzuri.acceleraudio.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class PreferencesActivity extends Activity implements OnCheckedChangeListener, OnItemSelectedListener {
  CheckBox useAccelerometerX;
  CheckBox useAccelerometerY;
  CheckBox useAccelerometerZ;
  CheckBox useMicrophone;
  CheckBox useGPS;
  Spinner recordLength;
  Spinner samplingRate;
  Spinner accelerometerRate;
  
  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    Log.i("PreferencesActivity", "onCreate(" + (icicle == null ? "null" : icicle) + ")");
    SharedPreferences preferences = this.getSharedPreferences("", MODE_PRIVATE);
    
    // Imposta il contenuto della activity prendendolo dal file res/layout/activity_preferences.xml
    setContentView(R.layout.activity_preferences);

    useAccelerometerX = (CheckBox) findViewById(R.id.useAccelerometerX);
    useAccelerometerY = (CheckBox) findViewById(R.id.useAccelerometerY);
    useAccelerometerZ = (CheckBox) findViewById(R.id.useAccelerometerZ);
    useMicrophone = (CheckBox) findViewById(R.id.useMicrophone);
    useGPS = (CheckBox) findViewById(R.id.useGPS);
    recordLength = (Spinner) findViewById(R.id.recordLength);
    samplingRate = (Spinner) findViewById(R.id.samplingRate);
    accelerometerRate = (Spinner) findViewById(R.id.accelerometerRate);

    ((ArrayAdapter<?>)samplingRate.getAdapter())
      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ((ArrayAdapter<?>)recordLength.getAdapter())
      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ((ArrayAdapter<?>)accelerometerRate.getAdapter())
      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    // imposta i vari controlli di questa schermata usando i valori trovati in preferences
    useAccelerometerX.setChecked(preferences.getBoolean("useAccelerometerX", true));
    useAccelerometerY.setChecked(preferences.getBoolean("useAccelerometerY", true));
    useAccelerometerZ.setChecked(preferences.getBoolean("useAccelerometerZ", true));
    useMicrophone.setChecked(preferences.getBoolean("useMicrophone", true));
    useGPS.setChecked(preferences.getBoolean("useGPS", true));
    recordLength.setSelection(Arrays.binarySearch(
        getResources().getIntArray(R.array.record_lengths),
        preferences.getInt("recordLength", 3)));
    samplingRate.setSelection(Arrays.binarySearch(
        getResources().getIntArray(R.array.sampling_rates),
        preferences.getInt("samplingRate", 8000)));
    accelerometerRate.setSelection(preferences.getInt("accelerometerRate", 3));
    
    useAccelerometerX.setOnCheckedChangeListener(this);
    useAccelerometerY.setOnCheckedChangeListener(this);
    useAccelerometerZ.setOnCheckedChangeListener(this);
    useMicrophone.setOnCheckedChangeListener(this);
    useGPS.setOnCheckedChangeListener(this);
    recordLength.setOnItemSelectedListener(this);
    samplingRate.setOnItemSelectedListener(this);
    accelerometerRate.setOnItemSelectedListener(this);
  }
  
  @Override protected void onPause() {
    saveValues();
    super.onPause();
  }
  
  private void saveValues() {
    SharedPreferences preferences = this.getSharedPreferences("", MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putBoolean("useAccelerometerX", useAccelerometerX.isChecked());
    editor.putBoolean("useAccelerometerY", useAccelerometerY.isChecked());
    editor.putBoolean("useAccelerometerZ", useAccelerometerZ.isChecked());
    editor.putBoolean("useMicrophone", useMicrophone.isChecked());
    editor.putBoolean("useGPS", useGPS.isChecked());
    editor.putInt("recordLength", getResources().getIntArray(R.array.record_lengths)[recordLength.getSelectedItemPosition()]);
    editor.putInt("samplingRate", getResources().getIntArray(R.array.sampling_rates)[samplingRate.getSelectedItemPosition()]);
    editor.putInt("accelerometerRate", accelerometerRate.getSelectedItemPosition());
    
    boolean success = editor.commit();
    if (!success) {
      throw new RuntimeException("Couldn't write preference file!");
    }
  }

  @Override public void onCheckedChanged(CompoundButton v, boolean isChecked) {
    saveValues();
  }

  @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    saveValues();
  }

  @Override public void onNothingSelected(AdapterView<?> parent) { }

}
